package site.zhouinfo.spider;


import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.auth.AuthScope;
import org.apache.commons.httpclient.auth.CredentialsProvider;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;

import java.io.IOException;

/**
 * 用来获取网内容信息
 * Author: zhouinfo
 * mySite: www.zhouinfo.site
 * Create Date：2016-03-22 22:47
 */
public class HttpGetUrl {

	private HttpClient httpClient = new HttpClient();

	public void getUrlInfo(String url) {
		GetMethod getMethod = new GetMethod(url);
		try {
			int statusCode = httpClient.executeMethod(getMethod);
			if (statusCode == HttpStatus.SC_OK) {
				System.out.println(getMethod.getResponseBodyAsString());
			}
			getMethod.releaseConnection();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void postUrlInfo(String url) {
		PostMethod postMethod = new PostMethod(url);
		NameValuePair[] postData = new NameValuePair[2];
		postData[0] = new NameValuePair("name", "zhouinfo");
		postData[1] = new NameValuePair("password", "zhouloop");
		postMethod.addParameters(postData);
		try {
			int statusCode = httpClient.executeMethod(postMethod);
			System.out.println(postMethod.getResponseBodyAsString());
			postMethod.releaseConnection();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void proxyUrl(String url) {
		httpClient.getHostConfiguration().setHost("192.168.0.1", 9527);
		httpClient.getParams().setAuthenticationPreemptive(true);
		httpClient.getParams().setParameter(CredentialsProvider.PROVIDER, new HttpGetUrl());
	}

}
